package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xc.erp.common.utils.StringUtils;
import com.xc.erp.modules.erp.domain.dto.DepotParam;

import com.xc.erp.common.utils.SecurityUtils;

import com.xc.erp.modules.erp.mapper.JshDepotMapper;
import com.xc.erp.modules.erp.mapper.JshUserMapper;
import com.xc.erp.modules.erp.domain.pojo.JshDepot;
import com.xc.erp.modules.erp.domain.pojo.JshUser;
import com.xc.erp.modules.erp.service.JshDepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【jsh_depot(仓库表)】的数据库操作Service实现
 * @createDate 2022-12-10 09:28:46
 */
@Service
public class JshDepotServiceImpl extends ServiceImpl<JshDepotMapper, JshDepot>
        implements JshDepotService {

    @Autowired
    private JshUserMapper jshUserMapper;

    @Override
    public List<JshDepot>  depotList() {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            //看这个用户有没有仓库权限
            Long count = jshUserMapper.selectCount(new QueryWrapper<JshUser>().eq("tenant_id", userId).eq("delete_flag", "0"));
            if (count > 0) {
                return list(new QueryWrapper<JshDepot>().eq("tenant_id", userId));
            }
        }else{
            return list();
        }

        return null;
    }

    @Override
    public boolean checkIsNameExist(String name) {
        return count(new QueryWrapper<JshDepot>().eq("name", name)) < 0;
    }

    @Override
    public boolean updateStatus(String[] ids, boolean status) {
       return update(new UpdateWrapper<JshDepot>().in("id",ids).set("enabled",status));
    }

    @Override
    public boolean delete(String ids[]) {
        return update(new UpdateWrapper<JshDepot>().in("id",ids).set("delete_flag",1));
    }

    @Override
    public boolean add(JshDepot jshDepot) {
        return save(jshDepot);
    }

    @Override
    public boolean defaultUpdateDepot(long id, boolean isDefault) {
        UpdateWrapper<JshDepot> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_default",isDefault);
        return update(wrapper);
    }

    @Override
    public IPage<JshDepot> listPage(Integer page, Integer limit, DepotParam depotParam) {
        IPage<JshDepot> pageModel = new Page<>(page,limit);
        QueryWrapper<JshDepot> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(depotParam.getName())){
            wrapper.like("name",depotParam.getName());
        }
        if(!StringUtils.isEmpty(depotParam.getRemark())){
            wrapper.like("remark",depotParam.getRemark());
        }
        wrapper.orderByAsc("sort");
        pageModel = page(pageModel, wrapper);
        pageModel.getRecords().stream().forEach(depot -> {
            QueryWrapper<JshUser> wrapper1 = new QueryWrapper<JshUser>().eq("id", depot.getPrincipal());
            depot.setJshUser(jshUserMapper.selectOne(wrapper1));
        });
        return pageModel;
    }
}




